//
//  ZBPopViewContainer.m
//  YouHui
//
//  Created by  on 11-12-13.
//  Copyright (c) 2011年 netease. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "ZBPopViewContainer.h"
#import "ZBAlertView.h"
#import "ZBInputAlertView.h"

#define TOPPAD 15.0
#define BOTTOMPAD 10.0
#define SPACE 10.0
#define SEP_TAG 152789
#define BUTTON_HEIGHT 30
#define INPUT_HEIGHT 30

static CGFloat buttonWidthOfCount(NSUInteger count) {
    if (count<=1) {
        return 107;
    } else if (count==2) {
        return 80;
    } else {
        return (280-(10*(count+1)))/count;
    }
}

@implementation ZBAlertViewContainerBase 
-(void)drawRect:(CGRect)rect {
    CGFloat colors[] = {0xf2/255.0,0xbc/255.0,0x4d/255.0,1.0,
        0xf2/255.0,0xa8/255.0,0x40/255.0,1.0
    };  
    CGFloat locations[] = {0.0,1.0};
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
	CGGradientRef gradient = CGGradientCreateWithColorComponents (colorspace, colors,
                                                                  locations, 2);
	
	CGPoint startPoint = self.bounds.origin;
	CGPoint endPoint = CGPointMake(startPoint.x,startPoint.y+self.bounds.size.height);
    CGContextRef context =UIGraphicsGetCurrentContext();
	CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);    
    CGColorSpaceRelease(colorspace);
    CGGradientRelease(gradient);
}
@end


@implementation ZBAlertViewContainer
@synthesize parentPopView;
-(void)addElements {
    for (UIView *subView in [self subviews]) {
        [subView removeFromSuperview];
    }
    [self addSubview:self.parentPopView.titleIcon];
    [self addSubview:self.parentPopView.titleLabel];
    [self addSubview:self.parentPopView.messageLabel];
    for (UIButton *button in self.parentPopView.buttons) {
        [self addSubview:button];
    }
    
    CGSize titleSize = [self.parentPopView.titleLabel.text sizeWithFont:self.parentPopView.titleLabel.font];
    self.parentPopView.titleLabel.frame = CGRectMake(0, 0, titleSize.width, titleSize.height);
    
    CGSize messageSize = [self.parentPopView.messageLabel.text sizeWithFont:self.parentPopView.messageLabel.font constrainedToSize:CGSizeMake(250, 200)];
    self.parentPopView.messageLabel.frame = CGRectMake(0, 0, messageSize.width, messageSize.height);
    
    CGSize iconSize = CGSizeZero;
    if (self.parentPopView.titleIcon.image) {
        iconSize = self.parentPopView.titleIcon.image.size;
    } 
    self.parentPopView.titleIcon.frame = CGRectMake(0, 0, iconSize.width, iconSize.height);
    
    
    CGFloat height = TOPPAD ;
    CGFloat titleHeight = MAX(titleSize.height, iconSize.height);
    if (titleHeight>0) {
        height += titleHeight;
        height += SPACE;
    }    
    if (messageSize.height>0) {
        height += messageSize.height;
        height += SPACE;
    }  
    
    if (self.parentPopView.sepColor) {
        height+=SPACE+1;
        UIView *sepView =[[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width,1)] autorelease];
        sepView.backgroundColor = self.parentPopView.sepColor;
        sepView.opaque = NO;
        sepView.tag = SEP_TAG;
        [self addSubview:sepView];
    }
    
    height += BUTTON_HEIGHT;
    height += BOTTOMPAD;
    
    CGRect frame = self.frame;
    frame.size.width = 280;
    frame.size.height = height;
    self.frame = frame;
    
    self.layer.borderWidth = 2.0;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
}

-(void)layoutSubviews {
    CGFloat widthBound = self.bounds.size.width;
    CGFloat x=0;
    CGFloat y = TOPPAD;
    CGRect iconRect = self.parentPopView.titleIcon.frame;
    CGRect titleRect = self.parentPopView.titleLabel.frame;
    if (iconRect.size.height>0||titleRect.size.height>0) {
        CGFloat innnerSpace = iconRect.size.height>0&&titleRect.size.height>0?2.0:0.0;
        CGFloat lineHeight = MAX(iconRect.size.height, titleRect.size.height);
        x = (widthBound-iconRect.size.width-titleRect.size.width-innnerSpace)/2;
        iconRect.origin.x = x;
        iconRect.origin.y = y + (lineHeight-iconRect.size.height)/2;
        self.parentPopView.titleIcon.frame = iconRect; 
        
        titleRect.origin.x = x+innnerSpace+iconRect.size.width;
        titleRect.origin.y = y+(lineHeight-titleRect.size.height)/2;;
        self.parentPopView.titleLabel.frame = titleRect; 
        
        y += lineHeight+SPACE;
    }
    
    CGRect messageRect = self.parentPopView.messageLabel.frame;
    if (messageRect.size.height>0) {
        x = (widthBound-messageRect.size.width)/2; 
        messageRect.origin.x = x;
        messageRect.origin.y = y;
        self.parentPopView.messageLabel.frame = messageRect;
        y += messageRect.size.height + SPACE;
    }
    
    UIView *sepView = [self viewWithTag:SEP_TAG];
    if (sepView) {
        sepView.frame = CGRectMake(0, y, widthBound, 1);
        y+=sepView.frame.size.height+SPACE;
    }
    
    CGFloat buttonWidth = buttonWidthOfCount([self.parentPopView.buttons count]);
    CGFloat buttonSpace  = (widthBound-buttonWidth*[self.parentPopView.buttons count])/([self.parentPopView.buttons count]+1);
    CGRect buttonRect = CGRectMake(0, 0, buttonWidth, BUTTON_HEIGHT);
    buttonRect.origin.x = buttonSpace;
    buttonRect.origin.y = y;
    int index = 0;
    for (UIButton *button in self.parentPopView.buttons) {
        buttonRect.origin.x += (buttonSpace+buttonWidth)*index;
        button.frame = buttonRect;
        index++;
    }
}
@end

@implementation ZBInputAlertViewContainer
@synthesize parentPopView;
-(void)addElements {
    for (UIView *subView in [self subviews]) {
        [subView removeFromSuperview];
    }
    [self addSubview:self.parentPopView.titleIcon];
    [self addSubview:self.parentPopView.titleLabel];
    [self addSubview:self.parentPopView.inputTextField];
    for (UIButton *button in self.parentPopView.buttons) {
        [self addSubview:button];
    }
    
    CGSize titleSize = [self.parentPopView.titleLabel.text sizeWithFont:self.parentPopView.titleLabel.font];
    self.parentPopView.titleLabel.frame = CGRectMake(0, 0, titleSize.width, titleSize.height);
    
    self.parentPopView.inputTextField.frame = CGRectMake(0, 0, 220, INPUT_HEIGHT);
    
    CGSize iconSize = CGSizeZero;
    if (self.parentPopView.titleIcon.image) {
        iconSize = self.parentPopView.titleIcon.image.size;
    } 
    self.parentPopView.titleIcon.frame = CGRectMake(0, 0, iconSize.width, iconSize.height);
    
    
    CGFloat height = TOPPAD ;
    CGFloat titleHeight = MAX(titleSize.height, iconSize.height);
    if (titleHeight>0) {
        height += titleHeight;
        height += SPACE;
    }    
    height += INPUT_HEIGHT;
    height += SPACE;

    if (self.parentPopView.sepColor) {
        height+=SPACE+1;
        UIView *sepView =[[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width,1)] autorelease];
        sepView.backgroundColor = self.parentPopView.sepColor;
        sepView.opaque = NO;
        sepView.tag = SEP_TAG;
        [self addSubview:sepView];
    }
    
    height += BUTTON_HEIGHT;
    height += BOTTOMPAD;
    
    CGRect frame = self.frame;
    frame.size.width = 280;
    frame.size.height = height;
    self.frame = frame;
    
    self.layer.borderWidth = 2.0;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
}

-(void)layoutSubviews {
    CGFloat widthBound = self.bounds.size.width;
    CGFloat x=0;
    CGFloat y = TOPPAD;
    CGRect iconRect = self.parentPopView.titleIcon.frame;
    CGRect titleRect = self.parentPopView.titleLabel.frame;
    if (iconRect.size.height>0||titleRect.size.height>0) {
        CGFloat innnerSpace = iconRect.size.height>0&&titleRect.size.height>0?2.0:0.0;
        CGFloat lineHeight = MAX(iconRect.size.height, titleRect.size.height);
        x = (widthBound-iconRect.size.width-titleRect.size.width-innnerSpace)/2;
        iconRect.origin.x = x;
        iconRect.origin.y = y + (lineHeight-iconRect.size.height)/2;
        self.parentPopView.titleIcon.frame = iconRect; 
        
        titleRect.origin.x = x+innnerSpace+iconRect.size.width;
        titleRect.origin.y = y+(lineHeight-titleRect.size.height)/2;;
        self.parentPopView.titleLabel.frame = titleRect; 
        
        y += lineHeight+SPACE;
    }
    
    CGRect inputRect = self.parentPopView.inputTextField.frame;
    x = (widthBound-inputRect.size.width)/2; 
    inputRect.origin.x = x;
    inputRect.origin.y = y;
    self.parentPopView.inputTextField.frame = inputRect;
    y += inputRect.size.height + SPACE;
    
    UIView *sepView = [self viewWithTag:SEP_TAG];
    if (sepView) {
        sepView.frame = CGRectMake(0, y, widthBound, 1);
        y+=sepView.frame.size.height+SPACE;
    }
    
    CGFloat buttonWidth = buttonWidthOfCount([self.parentPopView.buttons count]);
    CGFloat buttonSpace  = (widthBound-buttonWidth*[self.parentPopView.buttons count])/([self.parentPopView.buttons count]+1);
    CGRect buttonRect = CGRectMake(0, 0, buttonWidth, BUTTON_HEIGHT);
    buttonRect.origin.x = buttonSpace;
    buttonRect.origin.y = y;
    int index = 0;
    for (UIButton *button in self.parentPopView.buttons) {
        buttonRect.origin.x += (buttonSpace+buttonWidth)*index;
        button.frame = buttonRect;
        index++;
    }
}
@end

